"""
@author: ty
@file: lianbiao.py
@time: 2022/12/7 16:11
"""
#定义一个节点类，用来等一下实现链表的创建
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution:
    #实现链表的反转
    def reverseList(self, head):
        pre, cur = None, head
        while cur:
            tmp = cur.next
            cur.next = pre
            pre = cur
            cur = tmp
        return pre

#把反转以后的链表打印出来
def print_linked_list(head):
    if not head or not head.next:
        return []
    result = []
    while head:
        result.append(head.val)
        head = head.next
    return result

# 调用节点类，创建一个链表： a->b->c，分别对应的就是：1，2，3
a = ListNode(1)
b = ListNode(2)
c = ListNode(3)
d = ListNode(4)
a.next = b
b.next = c
c.next = d
#创建一个实例化对象
demo = Solution()
print(print_linked_list(demo.reverseList(a)))